/**
 * 功能:TK_CUSTOMS 控制器类
 * 开发人员:yaren
 * 创建时间:2012/4/21 23:50:11
 */
package com.towker.frame.controller.basicData;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.model.basicData.Companies;
import com.towker.frame.model.basicData.Customs;
import com.towker.frame.model.sell.SellLine;
import com.towker.frame.service.basicData.CompaniesService;
import com.towker.frame.service.basicData.CustomsService;
import com.towker.frame.service.basicData.RegionsService;
import com.towker.frame.share.sys.user.UserShare;
import com.towker.web.MyRequest;

//TK_CUSTOMS
@Controller
@SuppressWarnings("unchecked")
@RequestMapping("/manage/basicData")
public class CustomsController
{
	private CustomsService service;
	private RegionsService regionsService;
	private CompaniesService companiesService;

	//业务对象注入
	public void setCustomsService(CustomsService service)
	{
		this.service = service;
	}
	
	//业务对象注入
	public void setRegionsService(RegionsService regionsService)
	{
		this.regionsService = regionsService;
	}

	public void setCompaniesService(CompaniesService companiesService) {
		this.companiesService = companiesService;
	}

	//添加
	@RequestMapping("/addCustoms{viewName}")
	public ModelAndView addCustoms(HttpServletRequest request, HttpServletResponse response, Customs po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			Map map = new HashMap();
			map.put("region_type", 0);
			List regionsList = regionsService.getList(map);
			
			List companiesList = companiesService.getList(new HashMap());
			result.addObject("companiesList",companiesList);
			
			result.addObject("regionsList", regionsList);
			result.addObject("co_name", UserShare.getCurrentUserCoName(request));
			result.addObject("co_code", UserShare.getCurrentUserCoCode(request));
			result.setViewName("/manage/basicData/addCustoms.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				Map map = new HashMap();
				map.put("custom_name", po.getCustom_name());
				List list = service.getList(map);
				if (list.size()==0) {
					Companies co = companiesService.getByCode(po.getCo_code());
					if (co != null) {
						po.setCo_name(co.getCo_name());
						po.setCo_id(co.getId());
					}
					service.add(po);
					writer.write("添加客户成功！");
				}else {
					writer.write("添加客户失败,此客户名称已经存在！");
				}
				
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("添加客户失败！" + e.getCause());
			}
		}
		return result;
	}

	//删除
	@RequestMapping("/delCustoms")
	public void delCustoms(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect(req.getRefererURL());
	}

	//修改
	@RequestMapping("/updCustoms{viewName}")
	public ModelAndView updCustoms(HttpServletRequest request, HttpServletResponse response, Customs po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			MyRequest req = new MyRequest(request);
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			Map map = new HashMap();
			List regionsList = regionsService.getList(map);
			
			List companiesList = companiesService.getList(new HashMap());
			result.addObject("companiesList",companiesList);
			
			result.addObject("co_name", UserShare.getCurrentUserCoName(request));
			result.addObject("co_code", UserShare.getCurrentUserCoCode(request));
			
			result.addObject("regionsList", regionsList);
			result.addObject("po", po);
			result.setViewName("/manage/basicData/updCustoms.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				Map map = new HashMap();
				map.put("custom_name", po.getCustom_name());
				Map idMap = new HashMap();
				idMap.put("id", po.getId());
				idMap.put("custom_name", po.getCustom_name());
				List idList = service.getList(idMap);
				List list = service.getList(map);
				if (list.size()==0 || idList.size()==1) {
					Companies co = companiesService.getByCode(po.getCo_code());
					if (co != null) {
						po.setCo_name(co.getCo_name());
						po.setCo_id(co.getId());
					}
					service.update(po);
					writer.write("修改客户成功！");
				}else {
					writer.write("修改客户失败,此客户名称已经存在！");
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("修改客户失败！" + e.getCause());
			}
		}
		return result;
	}

	//获得分页
	@RequestMapping("/getCustoms")
	public ModelAndView getCustoms(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page)
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		//只查询当前用户所属公司
		map.put("co_code", UserShare.getCurrentUserCoCode(request));
		PageModel<Customs> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		Map map2 = new HashMap();
		List regionsList = regionsService.getList(map2);
		result.addObject("regionsList", regionsList);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/basicData/getCustoms.jsp");
		return result;
	}

	//明细
	@RequestMapping("/getCustomsById")
	public ModelAndView getCustomsById(HttpServletRequest request, HttpServletResponse response)
	{
		Customs po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		Map map = new HashMap();
		List regionsList = regionsService.getList(map);
		result.addObject("regionsList", regionsList);
		result.addObject("po", po);
		result.setViewName("/manage/basicData/getCustomsById.jsp");
		return result;
	}
	
	public String getJSON(HttpServletRequest request, HttpServletResponse response) {
		return "";
	}
	@ResponseBody
	@RequestMapping("/checkExistsCustomCode")
	public String checkExistsCode(HttpServletRequest request, HttpServletResponse response, Customs po){
		boolean result = service.checkExistsCode(po.getCustom_code(), po.getId());
		if (result) {
			return "{\"result\":1}";
		} else {
			return "{\"result\":0}";
		}
	}
}
